package com.kotlin.hxh.ui.fragment.second.child.childpager

import android.os.Bundle
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kotlin.hxh.R
import com.kotlin.hxh.adapter.HomeAdapter
import com.kotlin.hxh.base.BaseMainFragment
import com.kotlin.hxh.entity.Article
import com.kotlin.hxh.event.TabSelectedEvent
import com.kotlin.hxh.listener.OnItemClickListener
import com.kotlin.hxh.ui.activity.HxhActivity
import com.kotlin.hxh.ui.fragment.second.child.DetailFragment
import kotlinx.android.synthetic.main.zhihu_fragment_second_pager_first.*
import me.yokeyword.eventbusactivityscope.EventBusActivityScope

import org.greenrobot.eventbus.Subscribe
import java.util.*

/**
 * @author huxh
 * @date 2018/12/19.
 */
/**
 * Created by YoKeyword on 16/6/3.
 */
class FirstPagerFragment : BaseMainFragment(), androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener, OnItemClickListener {
    private var mAtTop = true
    private var mScrollTotal: Int = 0
    private var mTitles: Array<String>? = null
    private var mContents: Array<String>? = null
    private val mAdapter: HomeAdapter by lazy {
        HomeAdapter()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.zhihu_fragment_second_pager_first, container, false)
        EventBusActivityScope.getDefault(_mActivity).register(this)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    private fun initView() {

        mTitles = resources.getStringArray(R.array.array_title)
        mContents = resources.getStringArray(R.array.array_content)

        refresh_layout.setColorSchemeResources(R.color.colorPrimary)
        refresh_layout.setOnRefreshListener(this)

        val manager = androidx.recyclerview.widget.LinearLayoutManager(_mActivity)
        recy.layoutManager = manager
        recy.adapter = mAdapter

        mAdapter.setOnItemClickListener(this)

        // Init Datas
        val articleList = ArrayList<Article>()
        for (i in 0..14) {
            val index = (Math.random() * 3).toInt()
            val article = Article(mTitles!![index], mContents!![index])
            articleList.add(article)
        }
        mAdapter.setDatas(articleList)

        recy.addOnScrollListener(object : androidx.recyclerview.widget.RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: androidx.recyclerview.widget.RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                mScrollTotal += dy
                if (mScrollTotal <= 0) {
                    mAtTop = true
                } else {
                    mAtTop = false
                }
            }
        })
    }

    override fun onItemClick(position: Int, view: View?, holder: androidx.recyclerview.widget.RecyclerView.ViewHolder) {
        if (position == -1) return
        // 这里的DetailFragment在flow包里
        // 这里是父Fragment启动,要注意 栈层级
        (parentFragment as SupportFragment).start(DetailFragment.newInstance(mAdapter.getItem(position).title!!))
    }

    override fun onRefresh() {
        refresh_layout.postDelayed({ refresh_layout.isRefreshing = false }, 2000)
    }

    private fun scrollToTop() {
        recy.smoothScrollToPosition(0)
    }

    /**
     * 选择tab事件
     */
    @Subscribe
    fun onTabSelectedEvent(event: TabSelectedEvent) {
        if (event.position != HxhActivity.SECOND) return

        if (mAtTop) {
            refresh_layout.isRefreshing = true
            onRefresh()
        } else {
            scrollToTop()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        EventBusActivityScope.getDefault(_mActivity).unregister(this)
    }

    companion object {

        @JvmStatic
        fun newInstance(): FirstPagerFragment {

            val args = Bundle()

            val fragment = FirstPagerFragment()
            fragment.arguments = args
            return fragment
        }
    }

}
